package org.erepublik.company.service;

import java.util.ArrayList;

import org.erepublik.company.CompanyForSale;
import org.erepublik.company.ECountry;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JSONCompanyForSaleService extends JSONProductService<CompanyForSale> {

    public JSONCompanyForSaleService() {
        super("companyForSale");
    }



    public CompanyForSale[] getCompanies(ECountry southAfrica) {
        update();
        if (southAfrica==null)
            return allProducts;
        
        return productMap.get(southAfrica);
    }



    @Override
    protected CompanyForSale[] getEmptyTable() {
        return new CompanyForSale[0];
    }

    @Override
    protected CompanyForSale[] getProductFromJSONArray(JSONArray jsonArray) throws JSONException {
        return getCompaniesFromJSONArray(jsonArray).toArray(new CompanyForSale[0]);
    }
    
    public static ArrayList<CompanyForSale> getCompaniesFromJSONArray(JSONArray jsonArray) throws JSONException {
        ArrayList<CompanyForSale> data = new ArrayList<CompanyForSale>();
        for (int i = 0; i < jsonArray.length(); i++) {
            data.add( CompanyForSale.getCompanyForSaleFromJSON( (JSONObject)jsonArray.get(i) ) );
        }
        return data;
    }  

}
